package com.getmethods;

import java.lang.reflect.Method;

public class MyReflectGetMethods {
    public static void main(String[] args) throws Exception {
        //获取class字节码文件对象
        Class clazz = Class.forName("com.getmethods.Student");

        //获取所有方法对象
        System.out.println("获取所有方法对象");
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        System.out.println("--------------------");

        //获取指定单个方法
        System.out.println("获取指定单个方法");
        Method eat = clazz.getDeclaredMethod("eat", String.class);
        System.out.println(eat);
        System.out.println("--------------------");
        //获取方法修饰符
        //获取方法抛出的异常
        System.out.println("获取方法抛出的异常");
        Class[] exceptionTypes =  eat.getExceptionTypes();

        System.out.println("--------------------");

        //方法的运行
        System.out.println("方法的运行");
        Student s = new Student();
        eat.setAccessible(true);
        eat.invoke(s,"汉堡包");
    }
}
